package scavenger.client;

import scavenger.client.Action;
import scavenger.client.GameClient;
import scavenger.client.GameState;
import scavenger.client.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Color;

public class scavGui extends Activity {
	private String userName;
	private int playerID;
	private float gpsX, gpsY;
	private GameState gameState;
	private String hostName;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //hostName = "144.167.150.224";
        hostName = "144.167.149.52";
        showMainMenu();
    }  
    
    private void showMainMenu(){
    	setContentView(R.layout.main);
    	
    	((Button) findViewById(R.id.btnLogin)).setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View arg0) {
                	login();
                }
            }
        );
    }
    private void login(){
    	//save user inputed name to the client
    	AutoCompleteTextView uName = (AutoCompleteTextView) findViewById(R.id.atxtUserName);
    	userName = uName.getText().toString();
    	playerID = userName.hashCode();

    	//register with server
    	submitAction("register");
    	
    	//move to the next menu
    	showActionsMenu();
    }
    private void showActionsMenu(){
    	setContentView(R.layout.actions2);
    	
    	((ImageView) findViewById(R.id.imgCurrentMissionImage)).setOnClickListener(
                new ImageView.OnClickListener() {
                    public void onClick(View arg0) {
                    	submitAction("submit");
                    }
                }
            );
    	
    	String mID = getCurrentMission();
    	
    	//setup display and event listener appropriate to current mission
    	if(mID.equals("1")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image1);
    	}
    	if(mID.equals("2")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image2);
    	}
    	if(mID.equals("3")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image3);
    	}
    	if(mID.equals("4")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image4);
    	}
    	if(mID.equals("5")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image5);
    	}
    	if(mID.equals("6")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image6);
    	}
    	if(mID.equals("7")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image7);
    	}
    	if(mID.equals("8")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image8);
    	}
    	if(mID.equals("9")){
    		ImageView mImg = (ImageView) findViewById(R.id.imgCurrentMissionImage);
    		mImg.setImageResource(R.drawable.image9);
    	}
    }
    
    private void submitAction(String actionID){

    	getGPSLocation();
    	
    	if( actionID.equals("register")){
    		
    		GameClient gc = new GameClient(hostName,19998,19999);
        	
        	gc.submitAction(new Action(userName, "register", gpsX, gpsY));

        	this.gameState = gc.getGameState();
        	
    	}else if( actionID.equals("submit")){
    		
    		GameClient gc = new GameClient(hostName,19998,19999);
        	
        	gc.submitAction(new Action(userName, "submit", gpsX, gpsY));

        	this.gameState = gc.getGameState();
        	
        	showActionsMenu();
    	}
    }
    
    private String getCurrentMission(){
    	//to get the players current mission and return it's string ID
    	//to be used in updating the clients display
    	return String.valueOf( gameState.getCurrentMission() );
    }
    
    private void getGPSLocation(){
    	//get the client's location and store them in this.gpsX,gpsY,gpsZ
    	this.gpsX = 20.0f;
    	this.gpsY = 25.0f;
    }
    
    private void showProgressMenu(){
    	//intended to display the progress of the player
    	//as well as the progress of the top several other players
    }
   
};